//
//  WMESetViewController.m
//  everyDay_duiTang
//
//  Created by 恩叔 on 16/2/29.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMESetViewController.h"
#import "WMEReckonClearData.h"

@interface WMESetViewController ()<UITableViewDataSource ,UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSArray *arr;
@end

@implementation WMESetViewController

- (void)dealloc
{
    [_tableView release];
    [_arr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"设置";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleDone target:self action:@selector(tiaozhuanghuiqu:)];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createtableView];
}
// tableView
- (void)createtableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    // 协议
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [self.tableView release];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [_tableView release];
}
// table个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    // 判断是否为第0位元素
    if (indexPath.row == 0) {
        NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
        CGFloat h = [WMEReckonClearData folderSizeAtPath:path];
        cell.textLabel.text = [NSString stringWithFormat:@"清除缓存       %.2fMB", h];
        cell.tag = 10000;
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
        // 提示控制器
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"清空缓存么 亲" message:@"清除缓存会导致下次运行程序的时候拖慢 确认删除么" preferredStyle:UIAlertControllerStyleAlert];
        
        
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"点击确认");
            [WMEReckonClearData clearCache:path];
            UITableViewCell *cell = [self.view viewWithTag:10000];
            CGFloat h = [WMEReckonClearData folderSizeAtPath:path];
            cell.textLabel.text = [NSString stringWithFormat:@"清除缓存       %.2fMB", h];
        }]];
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }]];
        
        [self presentViewController:alertController animated:YES completion:nil];
        
    }
}

- (void)tiaozhuanghuiqu:(UIBarButtonItem *)btn
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
